package com.bjpowernode.crm.settings.web.interceptor;

import com.bjpowernode.crm.commons.constant.Constant;
import com.bjpowernode.crm.settings.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author:luosheng
 * @Date:2023/1/24 12:13
 * @Description:
 */

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("执行了拦截器");
        //实现每个页面的安全验证
        //验证用户是否登录，没有登录跳转登录页面
        HttpSession session = httpServletRequest.getSession();
        User user = (User) session.getAttribute(Constant.SESSION_USER);

        if (user == null) {
            //这里就是说我们如果没有登录就直接拦截跳转到controller下/  就是我们的登录首页

            httpServletResponse.sendRedirect(httpServletRequest.getContextPath());//当我们手动重定向时，url必须加项目的名称(前面那个get....相当于就是"/crm")
            return false;
        }
        //然后去配置springmvc的拦截路径(都拦截谁)
            return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
